import {
	getUrlParam
} from '@/static/js/filter.js';
export default {
	data() {
		const currentDate = this.getDate({
			format: true
		})
		return {
			subjectArr: [],
			dietData: [],
			smokeData: [],
			drinkData: [],
			motionData: [],
			sleepData: [],
			environmentData: [],
			showModal: false,
			showContent: '',
			dialog: false,
			id: '',
			start: '',
			end: currentDate,
			start_date: '',
			showModal1: false, //选择日期弹框组件
			number: 1, //当前题目数
			selected: ''
		}
	},
	onLoad() {
		const _this = this;
		_this.selected = getUrlParam('selected');
		_this.question().then((reponse) => {
			if (getUrlParam('id')) {
				const _this = this;
				_this.id = getUrlParam('id');
				_this.$api.psychologyQuestionInfo(_this.id).then((res) => {
					if (res.data.returnCode == 200) {
						const data1 = res.data.result && res.data.result.newUserTopicsEntities;
						const data2 = res.data.result && res.data.result.newAppMentalHealthAssessmentEntity;
						_this.userId = data2.userId;
						_this.healthId = data2.healthId;
						_this.start_date = data2.fillFormDate;
						_this.editTid = data2.tid;
						const listdata = this.subjectArr[this.number - 1].adTopicsRepliesEntityList;
						_this.subjectArr.map((a) => {
							a.adTopicsRepliesEntityList.map((b) => {
								data1.map((c) => {
									if (b.id == c.titleId) {
										b.adTopicsRepliesEntityList.map((d) => {
											d.nid = c.id;
											if (c.userOption && c.userOption.split(',').length > 1) {
												//多选
												c.userOption && c.userOption.split(',').map((e) => {
													if (e == d.elections) {
														d.checked = true
													}
												})
											} else {
												//单选
												if (d.elections == c.userOption) {
													d.checked = true;
												}
											}
										})
									}
								})
							})
						})

					}

				})
			}
		})
	},
	watch: {
		start_date(v) {
			this.subjectArr.map((a) => {
				a.adTopicsRepliesEntityList.map((b) => {
					b.adTopicsRepliesEntityList.map((c) => {
						if (c.type == 104) {
							if (c.checked) {
								const listdata = this.subjectArr[1].adTopicsRepliesEntityList;
								if (c.elections == 'A') {
									// 如果吸烟调查选择A
									listdata[1].hidden = true
									listdata[2].hidden = true
									listdata[3].hidden = true
								} else if (c.elections == 'B') {
									// 如果吸烟调查选择B
									listdata[1].hidden = false
									listdata[2].hidden = false
									listdata[3].hidden = false
								} else if (c.elections == 'C') {
									// 如果吸烟调查选择C
									listdata[1].hidden = false
									listdata[2].hidden = false
									listdata[3].hidden = true
								}
							}
						}else if (c.type == 76) {
							const listdata = this.subjectArr[2].adTopicsRepliesEntityList;
							if (c.checked) {
								if (c.elections == 'A') {
									// 如果饮酒调查选择A
									listdata[1].hidden = true
									listdata[2].hidden = true
									listdata[3].hidden = true
									listdata[4].hidden = true
									listdata[5].hidden = true
								} else if (c.elections == 'B') {
									// 如果饮酒调查选择B
									listdata[1].hidden = false
									listdata[2].hidden = false
									listdata[3].hidden = false
									listdata[4].hidden = false
									listdata[5].hidden = true
								} else if (c.elections == 'C') {
									// 如果饮酒调查选择C
									listdata[1].hidden = true
									listdata[2].hidden = true
									listdata[3].hidden = true
									listdata[4].hidden = true
									listdata[5].hidden = false
								}
							}
						} else if (c.type == 65) {
							const listdata = this.subjectArr[3].adTopicsRepliesEntityList;
							if (c.checked) {
								if (c.elections == 'A') {
									// 如果运动调查选择A
									listdata[1].hidden = true
									listdata[2].hidden = true
									listdata[3].hidden = true
									listdata[4].hidden = true
									listdata[5].hidden = true
								} else {
									listdata[1].hidden = false
									listdata[2].hidden = false
									listdata[3].hidden = false
									listdata[4].hidden = false
									listdata[5].hidden = false
								}
							}
						}
					})
				})
			})
		}
	},
	methods: {
		//获取心理健康问卷
		question() {
			return new Promise((success, error) => {
				this.$api.psychologyQuestion('ids=18&ids=19&ids=20&ids=21&ids=22&ids=23').then((res) => {
					if (res) {
						const data = Object.keys(res.data.result).map((a) => {
							const aData = res.data.result[a].adTopicsRepliesEntityList.map((b) => {
								b.adTopicsRepliesEntityList.map((c) => {
									c.checked = false;
									return b
								})
								return b
							})
							return res.data.result[a]
						})
						this.subjectArr = data;
						success(res.data.result)
					} else {
						error('')
					}
				})
			})
		},
		//滑动开始
		touchStart(e) {
			this.startDataX = e.changedTouches[0].clientX;
			this.startDataY = e.changedTouches[0].clientY;
		},
		//滑动结束
		touchEnd(e) {
			const subX = e.changedTouches[0].clientX - this.startDataX;
			const subY = e.changedTouches[0].clientY - this.startDataY; //右滑
			if (subX > 100) {
				if (this.number >= 2) {
					this.number = this.number - 1
				} 
			} else if (subX < -100) { //左滑
				if (this.number <= 5) {
					this.number = this.number + 1
				} 
			}

		},
		// 选答案
		answer(aitem, item) {
			if(this.selected && this.selected == 1) {
				return
			}
			item.adTopicsRepliesEntityList.map((a) => {
				a.checked = false
				return a
			})
			aitem.checked = true;

			const listdata = this.subjectArr[this.number - 1].adTopicsRepliesEntityList;
			if (aitem.type == 104) {
				if (aitem.elections == 'A') {
					// 如果吸烟调查选择A
					listdata[1].hidden = true
					listdata[2].hidden = true
					listdata[3].hidden = true
				} else if (aitem.elections == 'B') {
					// 如果吸烟调查选择B
					listdata[1].hidden = false
					listdata[2].hidden = false
					listdata[3].hidden = false
				} else if (aitem.elections == 'C') {
					// 如果吸烟调查选择C
					listdata[1].hidden = false
					listdata[2].hidden = false
					listdata[3].hidden = true
				}
			} else if (aitem.type == 76) {
				if (aitem.elections == 'A') {
					// 如果吸烟调查选择A
					listdata[1].hidden = true
					listdata[2].hidden = true
					listdata[3].hidden = true
					listdata[4].hidden = true
					listdata[5].hidden = true
				} else if (aitem.elections == 'B') {
					// 如果吸烟调查选择B
					listdata[1].hidden = false
					listdata[2].hidden = false
					listdata[3].hidden = false
					listdata[4].hidden = false
					listdata[5].hidden = true
				} else if (aitem.elections == 'C') {
					// 如果吸烟调查选择C
					listdata[1].hidden = true
					listdata[2].hidden = true
					listdata[3].hidden = true
					listdata[4].hidden = true
					listdata[5].hidden = false
				}
			} else if (aitem.type == 65) {
				if (aitem.elections == 'A') {
					// 如果吸烟调查选择A
					listdata[1].hidden = true
					listdata[2].hidden = true
					listdata[3].hidden = true
					listdata[4].hidden = true
					listdata[5].hidden = true
				} else {
					listdata[1].hidden = false
					listdata[2].hidden = false
					listdata[3].hidden = false
					listdata[4].hidden = false
					listdata[5].hidden = false
				}
			}
		},
		checkboxAnswer(aitem, item) {
			if(this.selected && this.selected == 1) {
				return
			}
			aitem.checked = !aitem.checked
		},
		//判断是否有未选择答案的问题
		// determine() {
		// 	const aa = this.questionnaire.map((a,ai) => {
		// 		const bresult =  a.options.filter((b) => {
		// 			return b.checked2
		// 		})
		// 		a.bresult = bresult
		// 		return a
		// 	})
		// 	var aaArr = aa.filter((c,ci)=>{
		// 		return c.bresult.length <= 0
		// 	})
		// 	if(aaArr.length > 0){
		// 		this.showModal1 = true,
		// 		this.showContent = '请选择'+aaArr[0].title
		// 		return
		// 	}
		// },

		// 保存
		showModalbtn(e) {
			if (!this.start_date) {
				uni.showToast({
					title: '请选择日期',
					icon: 'none'
				})
				return
			}
			let appUserTopicsEntities = []
			this.subjectArr.map((a) => {
				var bData = a.adTopicsRepliesEntityList.map((b) => {
					let bresult = b.adTopicsRepliesEntityList.filter((c) => {
						return c.checked
					});
					let elections = ''
					let userValue = ''
					bresult.map((a) => {
						elections += a.elections + ',';
						userValue += a.value + ','
					})
					elections = elections.substring(0, elections.lastIndexOf(","));
					userValue = userValue.substring(0, userValue.lastIndexOf(","));
					appUserTopicsEntities.push({
						titleId: b.id,
						id: bresult.length > 0 && bresult[0].nid || b.adTopicsRepliesEntityList[0].nid,
						userOption: bresult.length > 0 ? elections : '',
						userValue: bresult.length > 0 ? userValue : '',
						userNumber: bresult.length > 0 ? bresult[0].numbers : ''
					})
				})
			})
			const record = {
				appUserTopicsEntities: appUserTopicsEntities,
				appUserTopicsTitleEntity: {
					healthId: localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).healthId || '',
					userId: localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).userId || '',
				},
				fillFormDate: this.start_date,
				source: 1,
				status: e,
				tid: [18, 19, 20, 21, 22, 23],
				upload: 1,
				userId: localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).userId || '',
				healthId: localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).healthId || '',
			}

			if (this.id) {
				record.id = Number(this.id);
				record.appUserTopicsTitleEntity.id = this.editTid
				this.$api.psychologyQuestionUpdate(record).then((res) => {
					uni.showToast({
						title: res.data.message,
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/healthStatus/navigation?current=0'
						})
					}, 2000)
				})
			} else {
				this.$api.psychologyQuestionAdd(record).then((res) => {
					if (res.data.returnCode == 200) {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/healthStatus/navigation?current=0'
							})
						}, 2000)
					}
				})
			}

		},
		Modalconfirm() {

		},
		// 日期
		bindDateChange(e) {
			this.start_date = e.target.value;
		},
		// 获取当前时间
		getDate(type) {
			const date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();

			if (type === 'start') {
				year = year - 60;
			} else if (type === 'end') {
				year = year + 2;
			}
			month = month > 9 ? month : '0' + month;;
			day = day > 9 ? day : '0' + day;
			return `${year}-${month}-${day}`;
		},
	}
}
